
package jpoco.android;

import android.app.Application;
import java.util.*;
import jpoco.client.*;
import jpoco.client.settings.*;
import android.graphics.*;

public class AndroidApplication extends Application
{
	private static final Map<String, ClientSettings> settingsMap = new LinkedHashMap<String, ClientSettings>();
	private static final Map<String, Client> clientMap = new LinkedHashMap<String, Client>();
	private static String currentProvider = "";

	static
	{
		System.getProperties().put("org.apache.commons.logging.Log", AndroidLog.class.getName());
		// System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "trace");
		// System.getProperties().put("org.apache.commons.logging.diagnostics.dest", "STDOUT");
		
		android.util.Log.i("", System.getProperties().toString());
		
	}
	
	public void onCreate()
	{
		super.onCreate();
		
		ClientSettings plaxo = new PlaxoClientSettings();
		plaxo.setOAuthCallbackUrl("http://jpoco.googlecode.com/svn/trunk/jpoco-www/redirect-to-android-app.html?provider=" + plaxo.getProviderName());
		addProvider(plaxo);
		
		ClientSettings myspace = new MySpaceClientSettings();
		myspace.setConsumerToken(new Token("http://www.myspace.com/430598353", "3179a835f6594aa886a0e71d490abda6"));
		myspace.setOAuthCallbackUrl("http://jpoco.googlecode.com/svn/trunk/jpoco-www/redirect-to-android-app.html?provider=" + myspace.getProviderName());
		addProvider(myspace);
		
		ClientSettings orkut = new OrkutClientSettings();
		orkut.setConsumerToken(new Token("todo", "todo"));
		orkut.setOAuthCallbackUrl("http://jpoco.googlecode.com/svn/trunk/jpoco-www/redirect-to-android-app.html?provider=" + orkut.getProviderName());
		addProvider(orkut);
		
		ClientSettings partuza = new PartuzaClientSettings();
		partuza.setConsumerToken(new Token("eddac6d7-e6e6-cbd8-a9f5-c6deefdcdce3", "a572d68f9864afde76c35231333fe72b"));
		partuza.setOAuthCallbackUrl("http://jpoco.googlecode.com/svn/trunk/jpoco-www/redirect-to-android-app.html?provider=" + partuza.getProviderName());
		addProvider(partuza);
		
	}
	
	public static int getDefaultFontSize()
	{
		return 18;
	}
	
	public static void setCurrentProvider(String providerName)
	{
		currentProvider = providerName;
	}
	
	public static String getCurrentProvider()
	{
		return currentProvider;
	}
	
	public static Client getCurrentClient()
	{
		return getClient(getCurrentProvider());
	}
	
	public static Typeface getDefaultTypeface()
	{
		return Typeface.DEFAULT;
	}
	
	public static Iterator<String> getProviderNames()
	{
		return settingsMap.keySet().iterator();
	}
	
	public static List<ClientSettings> getAllProviders()
	{
		return new ArrayList<ClientSettings>(settingsMap.values());
	}
	
	public static ClientSettings getClientSettings(String providerName)
	{
		return settingsMap.get(providerName);
	}

	public static Client getClient(String providerName)
	{
		return clientMap.get(providerName);
	}
	
	static private void addProvider(ClientSettings cs)
	{
		settingsMap.put(cs.getProviderName(), cs);
		
		Client client = new Client();
		client.setClientSettings(cs);
		
		clientMap.put(cs.getProviderName(), client);
		
	}
	
	public void onTerminate()
	{
		super.onTerminate();
		
		if (clientMap != null)
		{
			for ( String key : clientMap.keySet() )
			{
				Client client = clientMap.get(key);
				if (client != null)
				{
					client.shutdown();
				}
			}
			clientMap.clear();
			settingsMap.clear();
		}
	}
	
}
